package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags="套餐管理相关接口")
public class  SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐及所包含的菜品
     * @param setmealDTO
     * @return
     */
    //key: setmealCache::100
    //指定删除某个key对应的缓存数据
    @CacheEvict(cacheNames = "setmeal",key="#setmealDTO.categoryId")
    @PostMapping
    @ApiOperation("新增套餐")
    public Result<String> save (@RequestBody SetmealDTO setmealDTO){
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setmealService.pageQuary(setmealPageQueryDTO);
        return Result.success(pageResult);

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)//setmeal下缓存资源全部删除
    @DeleteMapping
    @ApiOperation("批量删除数据")

    public Result<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable  Long id){
        SetmealVO setmealVO=setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)//setmeal下缓存资源全部删除
    @PutMapping
    @ApiOperation("修改套餐")
    public Result<String> update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }


    /**
     * 套餐的起售或停售
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)//setmeal下缓存资源全部删除
    @PostMapping("/status/{status}")
    @ApiOperation("套餐的起售或停售")
    public Result<String> startOrStop(@PathVariable Integer status,Long id){
        setmealService.startOrStop(status,id);
        return Result.success();
    }


}
